﻿using KaroglanSharedLib;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace KaroglanVersionProfiler
{
    public class Config
    {
        public string version = "1.0.0";
        public HashSet<string> blackList = new HashSet<string>();
        public const string cfgPath = "./config.json";
        public string workSpace = Utils.GetBaseFolder();

        public string oldPath = "";
        public string newPath = "";
        public string lastBListPath = "";

        public HashSet<string> temp_Add = new HashSet<string>();
        public HashSet<string> temp_Deleted = new HashSet<string>();
        public HashSet<string> temp_Modified = new HashSet<string>();
        internal bool clearDirectoryAfterGenerate = true;
        internal bool openExploreAfterGenerate = true;

        public static Config Load()
        {
            if (File.Exists(cfgPath))
            {
                var cfg = File.ReadAllText(cfgPath);
                var result = JsonConvert.DeserializeObject<Config>(cfg);
                if (!string.IsNullOrEmpty(result.oldPath))
                {
                    if (!Directory.Exists(result.oldPath))
                    {
                        result.oldPath = "";
                    }
                }
                if (!string.IsNullOrEmpty(result.newPath))
                {
                    if (!Directory.Exists(result.newPath))
                    {
                        result.newPath = "";
                    }
                }
                result.Save();
                return result;
            }
            else
            {
                var result = new Config();
                result.Save();
                return result;
            }
        }

        public void Save()
        {
            var js = JsonConvert.SerializeObject(this, Formatting.Indented);
            File.WriteAllText(cfgPath, js);
        }
    }
}
